//
//  MineHeadTableViewCell.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/3.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "MineHeadTableViewCell.h"

@interface MineHeadTableViewCell ()

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *vipLabel;
@property (nonatomic, strong) UIView *tagView;

@end

@implementation MineHeadTableViewCell

- (void)setDict:(NSDictionary *)dict {
//    if ([dict[@"person_img"] length] > 0) {
//        [_headImage sd_setImageWithURL:[NSURL URLWithString:dict[@"person_img"]] placeholderImage:nil options:SDWebImageRetryFailed];
//    }
    _nameLabel.text = dict[@"realname"];
    
    CGFloat width = kGetStringWidth(dict[@"realname"], CGSizeMake(MAXFLOAT, ScreenX375(16)), Kfont(16));
    self.vipLabel.frame = CGRectMake(SCREEN_WIDTH / 2 + width / 2 + 2, ScreenX375(107) + STATUS_BAR_HEIGHT, ScreenX375(50), ScreenX375(12));
    NSString *string = [NSString stringWithFormat:@"V%@",dict[@"vip"]];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:string];
    [attributedStr addAttribute:NSFontAttributeName value:Kfont(10) range:NSMakeRange(1, string.length - 1)];
    self.vipLabel.attributedText = attributedStr;
    
    for (UIView *view in self.tagView.subviews) {
        [view removeFromSuperview];
    }
    if ([dict[@"type"] integerValue] == 1) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(75)) / 2, 0, ScreenX375(75), ScreenX375(20))];
        label.text = @"设计师";
        label.textColor = [UIColor whiteColor];
        label.font = Kfont(12);
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.borderColor = [UIColor whiteColor].CGColor;
        label.layer.borderWidth = 1;
        label.layer.cornerRadius = ScreenX375(10);
        label.layer.masksToBounds = YES;
        [self.tagView addSubview:label];
    }else {
        if ([dict[@"customer_type"] integerValue] < 10) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(75)) / 2, 0, ScreenX375(75), ScreenX375(20))];
            if ([dict[@"customer_type"] integerValue] == 1) {
                label.text = @"设计师";
            }else if ([dict[@"customer_type"] integerValue] == 2) {
                label.text = @"包装经理人";
            }else if ([dict[@"customer_type"] integerValue] == 3) {
                label.text = @"终端客户";
            }
            label.textColor = [UIColor whiteColor];
            label.font = Kfont(12);
            label.textAlignment = NSTextAlignmentCenter;
            label.layer.borderColor = [UIColor whiteColor].CGColor;
            label.layer.borderWidth = 1;
            label.layer.cornerRadius = ScreenX375(10);
            label.layer.masksToBounds = YES;
            [self.tagView addSubview:label];
        }else {
            NSArray *arr = @[];
            if ([dict[@"customer_type"] integerValue] == 12) {
                arr = @[@"设计师",@"包装经理人"];
            }else if ([dict[@"customer_type"] integerValue] == 13) {
                arr = @[@"设计师",@"终端客户"];
            }else if ([dict[@"customer_type"] integerValue] == 23) {
                arr = @[@"终端客户",@"包装经理人"];
            }
            CGFloat margin = (SCREEN_WIDTH - ScreenX375(155)) / 2;
            for (int i = 0; i < 2; i++) {
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(margin + i * ScreenX375(80), 0, ScreenX375(75), ScreenX375(20))];
                label.text = arr[i];
                label.textColor = [UIColor whiteColor];
                label.font = Kfont(12);
                label.textAlignment = NSTextAlignmentCenter;
                label.layer.borderColor = [UIColor whiteColor].CGColor;
                label.layer.borderWidth = 1;
                label.layer.cornerRadius = ScreenX375(10);
                label.layer.masksToBounds = YES;
                [self.tagView addSubview:label];
            }
        }
    }
}

+ (MineHeadTableViewCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"MineHeadTableViewCell";
    MineHeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[MineHeadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    self.bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(170) + STATUS_BAR_HEIGHT)];
    self.bgView.image = [UIImage imageNamed:@"personal_bg_head"];
    [self addSubview:self.bgView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(170) + STATUS_BAR_HEIGHT)];
    view.backgroundColor = REXADECIMALCOLOR(@"#ed223b");
    view.alpha = 0.3;
    [self addSubview:view];
    
    UIView *headBg = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(86)) / 2, STATUS_BAR_HEIGHT + ScreenX375(14), ScreenX375(86), ScreenX375(86))];
    headBg.backgroundColor = RGBACOLOR(255, 255, 255, 0.6);
    headBg.layer.cornerRadius = ScreenX375(43);
    headBg.clipsToBounds = YES;
    [self addSubview:headBg];
    
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(7), ScreenX375(7), ScreenX375(72), ScreenX375(72))];
    self.headImage.layer.cornerRadius = ScreenX375(36);
    self.headImage.clipsToBounds = YES;
    self.headImage.image = [UIImage imageNamed:@"头部背景"];
    [headBg addSubview:self.headImage];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(111) + STATUS_BAR_HEIGHT, SCREEN_WIDTH, ScreenX375(16))];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.font = Kfont(16);
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.nameLabel];
    
    self.vipLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.vipLabel.font = Kfont(12);
    self.vipLabel.textColor = [UIColor whiteColor];
    [self addSubview:self.vipLabel];
    
    self.tagView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(135) + STATUS_BAR_HEIGHT, SCREEN_WIDTH, ScreenX375(20))];
    self.tagView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.tagView];
    
    UIButton *infoBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(170 + STATUS_BAR_HEIGHT))];
    [infoBtn addTarget:self action:@selector(infoClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:infoBtn];
    
    UIButton *setBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(42), ScreenX375(9) + STATUS_BAR_HEIGHT, ScreenX375(30), ScreenX375(30))];
    [setBtn setImage:[UIImage imageNamed:@"设置"] forState:UIControlStateNormal];
    [setBtn addTarget:self action:@selector(setClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:setBtn];
    
    NSArray *nameArr = @[@"我的订单",@"服务收藏",@"店铺关注",@"最近浏览"];
    CGFloat width = SCREEN_WIDTH / 4;
    for (int i = 0; i < nameArr.count; i++) {
        UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake((width - ScreenX375(24)) / 2 + i * width, ScreenX375(189) + STATUS_BAR_HEIGHT, ScreenX375(24), ScreenX375(24))];
        icon.image = [UIImage imageNamed:nameArr[i]];
        [self addSubview:icon];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * width, ScreenX375(222) + STATUS_BAR_HEIGHT, width, ScreenX375(14))];
        nameLabel.textColor = REXADECIMALCOLOR(@"#333333");
        nameLabel.text = nameArr[i];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = Kfont(14);
        [self addSubview:nameLabel];
        
        UIButton *classBtn = [[UIButton alloc] initWithFrame:CGRectMake(i * width, ScreenX375(170 + STATUS_BAR_HEIGHT), width, ScreenX375(80))];
        classBtn.backgroundColor = [UIColor clearColor];
        classBtn.tag = 100 + i;
        [classBtn addTarget:self action:@selector(classClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:classBtn];
    }
}

- (void)infoClicked {
    if (_infoClick) {
        _infoClick();
    }
}

- (void)setClicked {
    if (_setClick) {
        _setClick();
    }
}

- (void)classClick:(UIButton *)btn{
    if (_indexClick) {
        _indexClick(btn.tag - 100);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
